package org.jimmy.tang.jee.xml.stax;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.log4j.Logger;

/**
 * @author jimmy
 * 
 */
public class StaxSample {

    private static final Logger logger = Logger.getLogger(StaxSample.class);

    /**
     * 
     * @param args
     * @throws IOException
     * @throws XMLStreamException
     */
    public static void main(String[] args) throws XMLStreamException,
            IOException {
        logger.debug("The xml result is : "
                + new String(stAXToXml(City.cities)));
    }

    /**
     *  
     * @param list
     * @return
     * @throws XMLStreamException
     */
    public static byte[] stAXToXml(List<City> list) throws XMLStreamException,
            IOException {
        byte[] bytes = null;
        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        PrintWriter writerXml = new PrintWriter(new OutputStreamWriter(bao));
        XMLOutputFactory xof = XMLOutputFactory.newInstance();

        try {
            XMLStreamWriter xmlsw = xof.createXMLStreamWriter(writerXml);

            xmlsw.writeStartDocument("UTF-8", "1.0");
            xmlsw.writeStartElement("cities");

            for (City po : list) {
                xmlsw.writeStartElement("city");

                xmlsw.writeStartElement("id");
                xmlsw.writeCharacters(String.valueOf(po.getId()));

                xmlsw.writeEndElement();

                xmlsw.writeStartElement("name");
                xmlsw.writeCharacters(po.getName());

                xmlsw.writeEndElement();

                xmlsw.writeEndElement();
            }

            xmlsw.writeEndElement();

            xmlsw.writeEndDocument();
            xmlsw.flush();
            xmlsw.close();
            bytes = bao.toByteArray();

        } finally {
            bao.close();
        }

        return bytes;
    }

    static class City {
        String id;
        String name;

        City(String id, String name) {
            this.id = id;
            this.name = name;
        }

        static List<City> cities;
        static {
            cities = new ArrayList<City>();
            for (int i = 1; i <= 10; i++) {
                cities.add(new City("id" + i, "name" + i));
            }
        }

        /**
         * @return the id
         */
        public String getId() {
            return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(String id) {
            this.id = id;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }

    }
}
